package method;


import util.JdbcUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class JdbcDemo {


    Connection con=null;
    PreparedStatement psm=null;
    ResultSet rs=null;


    public String checkUser(int id,String password,String role){

        ResultSet rs=null;
        try {

            con = JdbcUtil.getConnection();
            psm = con.prepareStatement("SELECT u_id,password,role FROM user");
            rs=psm.executeQuery();


            while(rs.next()){

                boolean id_true=false;
                boolean password_currect =false;
                boolean role_currect=false;

                int temp_id = rs.getInt("u_id");
                String temp_password = rs.getString("password").trim();
                String temp_role=rs.getString("role");

                if(id==temp_id){
                    if(password.equals(temp_password)){
                        id_true = true;
                        password_currect = true;
                    }else {
                        password_currect=false;
                    }
                }
                if(temp_role.equals(role)){
                    role_currect=true;
                }else {
                    role_currect=false;
                }
                if (id_true==true&&password_currect==true&&role_currect==true){
                    return "loginsuccess";
                }
            }

            rs.close();

        } catch (SQLException e1) {
            e1.printStackTrace();
        }
        return "hasNoUserName";

    }

}

